"""
TikHub Sora2视频生成器使用示例
"""

import os
import time
from src.processors.tikhub_sora2_video import TikHubSora2VideoGenerator


def example_usage():
    """
    TikHub Sora2视频生成器使用示例
    """
    # 初始化生成器
    generator = TikHubSora2VideoGenerator()
    
    # 示例1: 文字生成视频
    print("=== 文字生成视频示例 ===")
    prompt = "A beautiful sunset over the ocean with dolphins jumping"
    task_result = generator.create_video(prompt, orientation="landscape")
    
    if task_result:
        task_id = task_result["id"]
        print(f"任务已创建，任务ID: {task_id}")
        
        # 等待任务完成
        final_status = generator.wait_for_completion(task_id)
        if final_status and final_status["status"] == "succeeded":
            # 获取无水印视频详情
            generation_id = final_status["generations"][0]["id"]
            detail = generator.get_task_detail(generation_id=generation_id)
            if detail and "post" in detail:
                video_url = detail["post"]["attachments"][0]["url"]
                print(f"无水印视频链接: {video_url}")
        else:
            print("任务执行失败")
    else:
        print("创建视频任务失败")
    
    # 示例2: 图片生成视频
    print("\n=== 图片生成视频示例 ===")
    # 请确保提供一个有效的图片路径
    image_path = "data/images/example.jpg" 
    if os.path.exists(image_path):
        # 上传图片
        upload_result = generator.upload_image(image_path)
        if upload_result:
            media_id = upload_result["id"]
            print(f"图片上传成功，Media ID: {media_id}")
            
            # 使用图片生成视频
            prompt = "Make the cat in the image come to life and play"
            task_result = generator.create_video(prompt, orientation="portrait", media_id=media_id)
            
            if task_result:
                task_id = task_result["id"]
                print(f"任务已创建，任务ID: {task_id}")
                
                # 等待任务完成
                final_status = generator.wait_for_completion(task_id)
                if final_status and final_status["status"] == "succeeded":
                    # 获取无水印视频详情
                    generation_id = final_status["generations"][0]["id"]
                    detail = generator.get_task_detail(generation_id=generation_id)
                    if detail and "post" in detail:
                        video_url = detail["post"]["attachments"][0]["url"]
                        print(f"无水印视频链接: {video_url}")
                else:
                    print("任务执行失败")
            else:
                print("创建视频任务失败")
        else:
            print("图片上传失败")
    else:
        print(f"图片文件不存在: {image_path}")


if __name__ == "__main__":
    example_usage()